package com.softer.chengxin.sysrole.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.sysrole.dao.SysRoleDao;
import com.softer.chengxin.sysrole.entity.ModuleStreet;
import com.softer.chengxin.sysrole.entity.Role;
import com.softer.chengxin.sysrole.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao roleDao;
    @Override
    public List<ModuleStreet> getModuleStreet() {
        List<ModuleStreet> streetList = roleDao.getModuleStreet(0);
        if(null != streetList && streetList.size()>0){
            for (ModuleStreet street : streetList) {
                List<ModuleStreet> children = roleDao.getModuleStreet(street.getId());
                street.setChildren(children);
                if(null != children && children.size()>0){
                    for (ModuleStreet s : children) {
                        List<ModuleStreet> c = roleDao.getModuleStreet(s.getId());
                        s.setChildren(c);
                    }
                }
            }
            return streetList;
        }
        return new ArrayList<>();
    }

    @Override
    public Role getModuleByRoleId(String roleId) {
        return roleDao.getModuleByRoleId(roleId);
    }

    @Override
    public ManagerPageResult getList(Page page, String loginName) {
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult js = new JsonResult();
        int count = roleDao.getCount(loginName);
        mpr.setCount(count);
        if (count <= Integer.parseInt(Common.ZERO)) {
            return mpr;
        }
        List<Role> list = roleDao.getList(page, loginName);
        js.setDs(list);
        mpr.setJson(js);
        return mpr;
    }

    /**
     * 新增角色
     *
     * @param role 角色信息
     * @return 处理结果
     */
    @Override
    public CommonResult add(Role role) {
        //查询dicCode是已经否存在
        int count = roleDao.getCountByDicCode(role.getDicCode());
        if(count>0){
            return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败！角色已存在！");
        }
        //查询字典表角色名称
        String name = roleDao.getDicNameByDicCode(role.getDicCode());
        role.setRoleName(name);
        //保存角色表
        int num = roleDao.addRole(role);
        if(num >0) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "添加成功！");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败！");
    }
    /**
     * 编辑角色
     *
     * @param role 角色信息
     * @return 处理结果
     */
    @Override
    @Transactional
    public CommonResult update(Role role) {
        int num = roleDao.update(role);
        if(num >0) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功！");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败！");
    }

    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return 处理结果
     */
    @Override
    public CommonResult delete(int id) {
        //查询roleId是否被用户绑定过
        int count= roleDao.getUserCountByroleId(id);
        if(count > 0){
            return new CommonResult(ResultEnum.FAIL.getCode(), "已绑定过用户！无法删除");
        }
        int num = roleDao.delete(id);
        if(num >0) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功！");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败！");
    }
    /**
     * 用户分配角色时查看角色列表
     * @return 角色列表
     */
    @Override
    public List<Role> getUserRoleList(){
        return roleDao.getUserRoleList();
    }
    /**
     * 查询字典表角色字段，用来填充角色新增下拉框
     * @return 角色列表
     */
    @Override
    public List<Role> getDicInfoRoleList(){
        return roleDao.getDicInfoRoleList();
    }
}
